package com.weisy.signin.network.utils;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Random;

/**
 * @author weisy
 * @function
 * @date 2019/3/22 20:52
 */
public class StreamToolkit {
    private static String TAG = "StreamToolkit";

    public static String readLine(InputStream nis) throws IOException {
        StringBuilder sb = new StringBuilder();
        int c1 = 0;
        int c2 = 0;
        while (c2 != -1 && !(c1 == '\r' && c2 == '\n')) {
            c1 = c2;
            c2 = nis.read();
            sb.append((char) c2);
        }
        if (sb.length() == 0) {
            return null;
        }
        return sb.toString();
    }

    public static String readContent(InputStream nis) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(nis, Charset.forName("UTF-8")));
        StringBuilder sb = new StringBuilder();
        int c1 = 0;
        int c2 = 0;
        while (c2 != -1 && !(c1 == '\r' && c2 == '\n')) {
            c1 = c2;
            c2 = reader.read();
            sb.append((char) c2);
        }
        if (sb.length() == 0) {
            return null;
        }
        return sb.toString();
    }

    public static String addContentType(String suffix) {
        String type = "Content-Type: application/json; charset=UTF-8";
        if (suffix.endsWith(".html")) {
            type = "Content-Type: text/html";
        } else if (suffix.endsWith(".js")) {
            type = "Content-Type: application/x-javascript";
        } else if (suffix.endsWith(".css")) {
            type = "Content-Type: text/css";
        } else if (suffix.endsWith(".jpg")) {
            type = "Content-Type: image/jpeg";
        } else if (suffix.endsWith(".png")) {
            type = "Content-Type: image/png";
        } else if (suffix.endsWith(".ico")) {
            type = "Content-Type: image/x-icon";
        }
        Log.d(TAG, "type=$type");
        return type;
    }

    /**
     * 产生1024~65535范围的随机数
     */
    public static int getPort() {
        Random rand = new Random();
        int port = rand.nextInt(64511) + 1024;
        Log.d("--util--", "random port=$port");
        return port;
    }

    /**
     * 获取当前IP地址
     */
    public static String getLocalIp(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        String ip = "";
        if (ipAddress != 0) {
            ip = (ipAddress & 0xFF) + "." +
                    ((ipAddress >> 8) & 0xFF) + "." +
                    ((ipAddress >> 16) & 0xFF) + "." +
                    (ipAddress >> 24 & 0xFF);
        }
        if (ipAddress == 0 || ip.equals("0.0.0.0")) {
            ip = "192.168.43.1";
        }
        return ip;
    }
}
